var buildId;
var linkNum = "";
var isLogin;
var token;
var lat;
var lng;

var favoriteIcon = document.getElementById("favoriteIcon");
(function($) {

	mui.init({
		keyEventBind: {
			backbutton: true //开启back按键监听
		}
	});
	setPxPerRem();
	mui.plusReady(function() {
		plus.nativeUI.showWaiting("加载中");
		buildId = getQueryString("itemID");
		isLogin = localStorage.getItem("isLogin");
		token = localStorage.getItem("token_type") + " " + localStorage.getItem("access_token");
		if(isHaveNet()) {
			getBuildInfo(buildId);
		} else {
			mui.toast("网络连接失败，请稍后再试");
		}
		mui.back = function(event) {
			var ws = plus.webview.currentWebview();
			plus.webview.close(ws);
			return false;
		};
	});

})(mui);

function getBuildInfo(id) {
	//	var jsonData = {
	//		lat:'',
	//		lng:""
	//	}
	//	if(checkParams(jsonData)){
	//		mui.toast("请求失败，请稍后再试")
	//		return;
	//	}
	var opts = {
		url: "/building/" + id,
		addressUrl: "testline",
		requestType: "GET"
	}

	crossDomainAjax(opts, function(data) {
		if(!isEmpty(data)) {
			var row = data.data;
			if(!isEmpty(row)) {
				document.getElementById("titleText").innerHTML = row.name;
				document.getElementById("buildName").innerHTML = row.name;
				document.getElementById("browsed").innerHTML = row.browsed;
				//				document.getElementById("kzl").innerHTML = ((100.00 - row.vacancy_rate).toFixed(3));
				//				document.getElementById("jzmj").innerHTML = row.covered_acreage;
				//				document.getElementById("zsmj").innerHTML = row.investment_acreage;
				document.getElementById("contentHtml").innerHTML = row.content;
				lng = row.lat;
				lat = row.lng;
			}
			linkNum = row.linkmobile;
			var photos = row.photos;
			var imgStr = "";
			for(var i = 0; i < photos.length; i++) {

				imgStr = imgStr + '<div class="swiper-slide">' +
					'<img class="swiper-img swiper-lazy" src="' + photos[i] + '" />' +
					'</div>';

			}
			console.log(imgStr);
			document.getElementById("swiperImgs").innerHTML = imgStr;
			var swiper = new Swiper('.swiper-container', {
				pagination: '.swiper-pagination',
				paginationClickable: true,
				lazyLoading: true,
				autoplay: 4000,
			});
			var point;
			if(isEmpty(lat) || isEmpty(lng)) {
				point = new BMap.Point(120.389013, 36.073168);
			} else {
				point = new BMap.Point(lat, lng);
			}
			var map = new BMap.Map("container");
			map.centerAndZoom(point, 17);
			map.setCurrentCity("青岛"); // 设置地图显示的城市 此项是必须设置的
			map.disableDragging();
			map.disableScrollWheelZoom(true);
			var myIcon = new BMap.Icon("../../ui/building/images/05buildinfo/05.png", new BMap.Size(20, 25));
			var marker2 = new BMap.Marker(point, {
				icon: myIcon
			});
			map.addOverlay(marker2);
			if(isLogin && !isEmpty(token)) {
				getFavoriteType(token, buildId)
			} else {
				plus.nativeUI.closeWaiting();
				favoriteIcon.src = "../../ui/building/images/05buildinfo/01.png";
			}
		}
	});
}
document.getElementById("linkBtn").addEventListener('click', function() {
	if(isEmpty(linkNum)) {
		mui.toast("暂无联系人");
		return;
	}
}, false);

$("#favoriteIcon").click(function() {
	if(isLogin) {
		var srcPath = favoriteIcon.src;
		if(isHaveNet()) {
			if(srcPath.indexOf("05buildinfo/01.png") > -1) {
				favoriteBuild(token, buildId);
			} else {
				deleteFavorite(token, buildId);
			}
		} else {
			mui.toast("网络连接失败，请稍后再试");
		}
	} else {
		mui.toast("请登陆后在收藏");
	}
});

function getFavoriteType(authorization, id) {
	var urlPath = getRequestAddressUrl("testline") + "/user/is_favorite/building/" + id;
	console.log("urlPath==>>" + urlPath);
	console.log("authorization==>>" + authorization);
	var settings = {
		url: urlPath,
		method: "GET",
		headers: {
			"accept": "application/json",
			"authorization": authorization
		}
	}

	console.log("settings==>" + JSON.stringify(settings));

	$.ajax(settings).done(function(data) {
		plus.nativeUI.closeWaiting();
		showEle(favoriteIcon);
		if(!isEmpty(data)) {
			var meta = data.meta;
			if(!isEmpty(meta)) {
				if(meta.code == "10010") {
					$("#favoriteIcon").attr('src', "../../ui/building/images/05buildinfo/02.png");
					$("#favoriteText").html("取消");
				} else {
					$("#favoriteIcon").attr('src', "../../ui/building/images/05buildinfo/01.png")
					$("#favoriteText").html("收藏");
				}
			} else {
				$("#favoriteIcon").attr('src', "../../ui/building/images/05buildinfo/01.png")
				$("#favoriteText").html("收藏");
			}
		} else {
			$("#favoriteIcon").attr('src', "../../ui/building/images/05buildinfo/01.png")
			$("#favoriteText").html("收藏");
		}
	});
}

/**
 * 收藏楼宇
 * @param {Object} authorization
 * @param {Object} id
 */
function favoriteBuild(authorization, id) {
	var settings = {
		url: getRequestAddressUrl("testline") + "/user/favorite/building",
		method: "POST",
		data: {
			"id": id
		},
		headers: {
			"accept": "application/json",
			"authorization": authorization
		}
	}

	$.ajax(settings).done(function(data) {
		if(!isEmpty(data)) {
			var meta = data.meta;
			if(!isEmpty(meta)) {
				if(meta.code == "10000") {
					mui.toast("楼宇收藏成功");
					$("#favoriteIcon").attr('src', "../../ui/building/images/05buildinfo/02.png")
					$("#favoriteText").html("取消");
				} else {
					mui.toast("楼宇收藏失败");
					$("#favoriteIcon").attr('src', "../../ui/building/images/05buildinfo/01.png")
				}
			} else {
				mui.toast("楼宇收藏失败");
				$("#favoriteIcon").attr('src', "../../ui/building/images/05buildinfo/01.png")
			}
		} else {
			mui.toast("楼宇收藏失败");
			$("#favoriteIcon").attr('src', "../../ui/building/images/05buildinfo/01.png")
		}
	});
}

function deleteFavorite(authorization, id) {
	var settings = {
		url: getRequestAddressUrl("testline") + "/user/favorite/building/" + id,
		method: "POST",
		data: {
			"_method": "DELETE"
		},
		headers: {
			"accept": "application/json",
			"authorization": authorization
		}
	}

	$.ajax(settings).done(function(data) {
		if(!isEmpty(data)) {
			var meta = data.meta;
			if(!isEmpty(meta)) {
				if(meta.code == "10000") {
					mui.toast("已取消楼宇收藏");
					$("#favoriteIcon").attr('src', "../../ui/building/images/05buildinfo/01.png")
					$("#favoriteText").html("收藏");
				} else {
					mui.toast("取消楼宇收藏失败");
					$("#favoriteIcon").attr('src', "../../ui/building/images/05buildinfo/02.png")
				}
			} else {
				mui.toast("取消楼宇收藏失败");
				$("#favoriteIcon").attr('src', "../../ui/building/images/05buildinfo/02.png")
			}
		} else {
			mui.toast("取消楼宇收藏失败");
			$("#favoriteIcon").attr('src', "../../ui/building/images/05buildinfo/02.png")
		}
	});
}

$("#foodBtn").click(function() {
	var postData = {
		pageflag: "page_buildinfo.html",
		lng: lat,
		lat: lng
	}
	
	if(checkParams(postData)){
		mui.toast("定位失败");
		return;
	}
	var change = {
		pageUrl: 'page_food_map.html',
		postData: postData,
		aniShow: "fade-in"
	}
	pageChange(change);
});